package com.yuang.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.*;

/**
 * Hello world!
 *
 */

class TestClass implements Serializable {
    private String sname;
    private double price;
    private int count;

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public TestClass(String sname, double price, int count) {
        this.sname = sname;
        this.price = price;
        this.count = count;
    }
}

@Component
public class App {

    // RedisTemplate<Serializable, Serializable> 使用这种方式初始化才能序列化
    // 同时key可以支持中文
    @Autowired
    private RedisTemplate<Serializable, Serializable> redisTemplate;

    public void testString() {
        System.out.println("================testString===============");
        // generate redis obj
        ValueOperations<Serializable,Serializable> redisOpt = redisTemplate.opsForValue();
        redisOpt.set("city","北京");
        System.out.println("city:"+redisOpt.get("city"));

        //直接将对象存入redis中，对象需要序列化(实现序列化接口即可)
        ValueOperations<Serializable,Serializable> redis = redisTemplate.opsForValue();
        TestClass test = new TestClass("JAVA开发",50.00, 2);
        redisOpt.set("book",test);
        TestClass t = (TestClass)redis.get("book");
        System.out.println("反序列化后sname="+t.getSname());
        redisTemplate.delete("city");
        redisTemplate.delete("book");
    }

    public void testList() {
        System.out.println("================testList===============");
        ListOperations<Serializable,Serializable> redisOpt = redisTemplate.opsForList();
        redisOpt.leftPush("country","中国");
        redisOpt.leftPush("country","美国");
        redisOpt.rightPush("country","日本");
        redisOpt.rightPush("country","英国");

        //获取数据集,从第1个到倒数第1个(即所有数据)
        List<Serializable> range = redisOpt.range("country",0,-1);
        //System.out.println(range);
        for(int i=0; i < redisOpt.size("country"); i++)
            System.out.println(redisOpt.index("country",i));
        redisTemplate.delete("country");
    }

    public void testHash() {
        System.out.println("================testHash===============");
        HashOperations<Serializable, Serializable, Serializable> redisOpt = redisTemplate.opsForHash();
        Map<String,Serializable> map = new  HashMap<String,Serializable>();
        map.put("name","小明");
        map.put("age","30");
        map.put("hight","180");
        redisOpt.putAll("student",map);

        Set<Serializable> keys = redisOpt.keys("student");
        for(Object key : keys) {
            Object value = redisOpt.get("student", key);
            System.out.println(key+":"+value);
        }
        redisTemplate.delete("student");
    }

    public void testSet() {
        System.out.println("================testSet===============");
        SetOperations<Serializable,Serializable> redisOpt = redisTemplate.opsForSet();
        String[] arr =  new String[3];
        arr[0] = "中文";
        arr[1] = "日文";
        arr[2] = "英语";
        redisOpt.add("language",arr);
        Set<Serializable> members = redisOpt.members("language");
        System.out.println(members);
        redisTemplate.delete("language");
    }

    public void testSortedSet() {
        System.out.println("================testSortedSet===============");
        ZSetOperations<Serializable,Serializable> redisOpt = redisTemplate.opsForZSet();
        redisOpt.add("major","语文",1);
        redisOpt.add("major","数学",4);
        redisOpt.add("major","物理",2);
        Set<Serializable> members = redisOpt.rangeByScore("major",0,10);
        System.out.println(members);
        redisTemplate.delete("major");
    }

    public void getAllKeys() {
        System.out.println(redisTemplate.keys("account~keys"));
    }


    public static void main( String[] args )
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        // 从spring容器获取bean以便能够使用被容器初始化过的redisTemplate
        App app = (App)context.getBean("app");
        app.testString();
        app.testList();
        app.testHash();
        app.testSet();
        app.testSortedSet();
        //app.getAllKeys();
    }
}
